package com.hotelsystem.reservations.rooms;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;

import com.google.gson.Gson;
import com.hotelsystem.reservations.constants.Constants;
import com.hotelsystem.reservations.hotels.Hotel;
import com.hotelsystem.reservations.interfaces.RoomInterface;
import com.hotelsystem.reservations.persons.ClientManagement;

public class RoomManagement implements RoomInterface {
	/**
	 * This method creates a new Room class as a .json file in the directory:
	 * {@link RoomInterface#ROOMS_FOLDER_PATH}
	 * 
	 * @param room 
	 * @param hotel
	 * @author Konstantin
	 * @return true or false; weather the add has been successful
	 */
	public boolean addRoom(Room room, Hotel hotel) {
		Gson gson = new Gson();
		String roomJson = gson.toJson(room);
		File roomFile = new File(ROOMS_FOLDER_PATH, hotel.getHotelInformation()
				.getName() + "_" + room.getRoomNumber() + ".json");
		try (FileWriter writer = new FileWriter(roomFile)) {
			writer.write(roomJson);
			return true;
		} catch (IOException e) {
			System.out.println("File Not Found");
			e.printStackTrace();
		}
		return false;
	}

	public boolean removeRoom(Room room, Hotel hotel) {
		if (room == null || !hotel.getRooms().contains(room)) {
			return false;
		}
		hotel.getRooms().remove(room);
		return true;
	}

	public boolean updateRoomDetails(Room room, Hotel hotel) {
		if (room != null) {
			for (Room buf : hotel.getRooms()) {
				if (room.getRoomNumber() == buf.getRoomNumber()) {
					hotel.getRooms().add(hotel.getRooms().indexOf(buf) + 1,
							room);
					hotel.getRooms().remove(buf);
					return true;
				}
			}
		}
		return false;
	}
	
	public void deleteRoom (Room room){
		int roomIndex = room.getRoomNumber();
		String roomsPath = Constants.ROOMS_PATH;
		
		File directory = new File(roomsPath);
		
	    File [] files = directory.listFiles();
	    
	    for(File file : files) {
	    	System.out.println(file.getName());
			Gson gson = new Gson();				    
			String json;
			try {
				json = new String(Files.readAllBytes(Paths.get(roomsPath + file.getName())));
				room = gson.fromJson(json, room.getClass());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if(room.getRoomNumber() == roomIndex) {
				file.delete();
			}
	    }
	    
	    

	}
	
	public ArrayList<Room> getRooms() {
		
		String roomsPath = Constants.ROOMS_PATH;
		ArrayList<Room> rooms = new ArrayList<Room>();
		
		File directory = new File(roomsPath);
		if(!directory.exists()) {
			return null;
		}
		
		File[] roomsFiles = directory.listFiles();
		
		for(File file : roomsFiles) {
			Room room = new Room();
			Gson gson = new Gson();				    
			String json;
			try {
				json = new String(Files.readAllBytes(Paths.get(roomsPath + file.getName())));
				room = gson.fromJson(json, room.getClass());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			rooms.add(room);
		}
		
		return rooms;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println("sdadsa");
		RoomManagement m = new RoomManagement();
		ArrayList<Room> rooms = m.getRooms();
		
		for(Room r : rooms) {
			System.out.println(r);
		}
		
	}

}
